package com.chinasoft.movie.service.impl;

import com.chinasoft.movie.mapper.UserMapper;
import com.chinasoft.movie.service.IUserService;
import com.chinasoft.movie.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper mapper;

    @Override
    public List<Map<String, Object>> selectUserList(Map<String,Object> map) {
        return mapper.selectUserList(map);
    }

    @Override
    public int insertUser(Map<String, Object> map) {
        return mapper.insertUser(map);
    }

    @Override
    public int deleteUserByIds(List<Integer> list) {
        return mapper.deleteUserByIds(list);
    }

    @Override
    public int selectUserCount(Map<String, Object> map) {
        return mapper.selectUserCount(map);
    }

    @Override
    public int insertVideo(Map<String, Object> map) {
        return mapper.insertVideo(map);
    }

    @Override
    public int insertVideo1(List<Map<String, Object>> list) {
        return mapper.insertVideo1(list);
    }

    @Override
    public List<Map<String, Object>> selectVideoList() {
        List<Map<String,Object>> list=mapper.selectVideoList();
        //String name1= ToolUtil.getFileSuffixName(name);
        for(int i=0;i<list.size();i++){
            Map<String,Object> map=list.get(i);
            String name=map.get("videoMovieUrl").toString();
            String name1= ToolUtil.getFileSuffixName(name);
            map.put("suffixName",name1);
        }

        return list;
    }

    @Override
    public Map<String, Object> selectVideoById(Map<String, Object> map) {
        return mapper.selectVideoById(map);
    }

    @Override
    public String selectEmployeeLogin(Map<String, Object> map, HttpServletRequest request, ValueOperations redisValue) {
        String str=null;
        HttpSession session=request.getSession();
        if(mapper.selectEmployeeLogin(map)==null){
            //System.out.println("没有查询到数据");
            //接下来我要将这个信息放入到 session里面去
            session.setAttribute("message","账号有问题!");
            str="redirect:/index1";
        }else{
            //System.out.println("否则查询的有数据");
            Map<String,Object> dataMap=mapper.selectEmployeeLogin(map);//dataMap就是我根据用户的账号传递到map里面去查询出的这一条数据
            String dataBasePassword=dataMap.get("employeePassword").toString();
            String formPassword=map.get("employeePassword").toString();
            //System.out.println("dataBasePassword"+dataBasePassword);
            //System.out.println("formPassword"+formPassword);
            if(!dataBasePassword.equals(formPassword)){
                session.setAttribute("message","密码有问题!");
                str="redirect:/index1";
            }else{
                System.out.println("账号和密码都没问题");
                //System.out.println("sessionId"+session.getId());
                String employeeAccount=dataMap.get("employeeAccount").toString();//admin1
                List<String> list=new ArrayList<String>();
                list.add(employeeAccount);//admin1
                list.add(session.getId());//ERT456YURRR
                session.setAttribute("account",list);//放入session "account"
                //接着放入redis数据库
                redisValue.set(employeeAccount,session.getId());//admin1 ERT456YURRR
                str="index2";
            }
        }
        return str;
    }
}
